"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : GUC
Case Name   : 使用gs_guc set方法设置参数quote_all_identifiers值为on，
              观察预期结果
Create At   : 2020/12/2
Owner       : opentestcase005
Description :
        1.查询quote_all_identifiers默认值
        2.修改参数值为on并重启数据库
        3.查询修改后的参数值
        4.创建表和视图
        5.使用pg_get_viewdef查询视图；使用explain语句查询表
        6.恢复参数默认值
Expect      :
        1.显示默认值为on
        2.修改成功
        3.显示on
        4.创建成功
        5.查询结果中的视图名均加双引号
        6.默认值恢复成功
History     :
"""
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

LOG = Logger()
commonsh = CommonSH('dbuser')


class ClientConnection(unittest.TestCase):
    def setUp(self):
        LOG.info(
            '------Opengauss_Function_Guc_VPC_Case0008start----')
        self.constant = Constant()

    def test_quote_all_identifiers(self):
        LOG.info('---步骤1:查询默认值---')
        sql_cmd = commonsh.execut_db_sql('''show quote_all_identifiers;''')
        LOG.info(sql_cmd)
        self.res = sql_cmd.splitlines()[-2].strip()
        LOG.info('---步骤2:修改参数值为on---')
        msg = commonsh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     "quote_all_identifiers = on")
        LOG.info(msg)
        self.assertTrue(msg)
        LOG.info('---步骤3:重启数据库---')
        msg = commonsh.restart_db_cluster()
        LOG.info(msg)
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info('---步骤4:查询修改后的参数值---')
        sql_cmd = commonsh.execut_db_sql('''show quote_all_identifiers;''')
        LOG.info(sql_cmd)
        self.assertIn(self.constant.BOOLEAN_VALUES[0], sql_cmd)
        LOG.info('---步骤5:创建视图---')
        sql_cmd = commonsh.execut_db_sql('''drop table if exists t_0266;
            create table t_0266 (id int);
            drop view if exists v_0226;
            create view v_0266 as select * from t_0266;''')
        LOG.info(sql_cmd)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_cmd)
        self.assertIn(self.constant.CREATE_VIEW_SUCCESS_MSG, sql_cmd)
        LOG.info('---步骤6:使用pg_get_viewdef查询,强制引用所有的标识符---')
        sql_cmd = commonsh.execut_db_sql('''select pg_get_viewdef('v_0266');
            explain select * from t_0266;''')
        LOG.info(sql_cmd)
        self.assertIn('SELECT  * FROM "t_0266";', sql_cmd)
        self.assertIn('Seq Scan on "t_0266"', sql_cmd)

    def tearDown(self):
        LOG.info('---步骤7:清理环境---')
        sql_cmd = commonsh.execut_db_sql('''drop table if exists t_0266 
            cascade;''')
        LOG.info(sql_cmd)
        sql_cmd = commonsh.execut_db_sql('''show quote_all_identifiers;''')
        LOG.info(sql_cmd)
        if self.res != sql_cmd.split('\n')[2].strip():
            msg = commonsh.execute_gsguc('set',
                                         self.constant.GSGUC_SUCCESS_MSG,
                                         f'quote_all_identifiers={self.res}')
            LOG.info(msg)
            msg = commonsh.restart_db_cluster()
            LOG.info(msg)
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info(
            '------Opengauss_Function_Guc_VPC_Case0008finish----')
